<?php

/**
 * Homepage presenter.
 */
use Nette\Application\UI\Form;
class PackageInfoPresenter extends BasePresenter {
	
	private $packageInfoService;
	private $packageModel;
	private $roleModel;
	private $packageStateModel;
	private $result;
	
	protected function startup() {
		parent::startup ();
		$this->packageInfoService = $this->context->packageInfoService;
		$this->packageModel = $this->context->packageModel;
		$this->roleModel = $this->context->roleModel;
		$this->packageStateModel = $this->context->packageStateModel;
		
	}
	
	public function renderDefault() {
		
		
	}
	
	//zobrazí informace o zásilce pro zadané postingNumber
	public function renderShow($postingNumber){
		$result = $this->packageInfoService->getPackageInfo($postingNumber);
		$this->template->resultInfo = $result->getStateOfPackage();
		$result2 = $this->packageStateModel->savePackageState($this->packageModel->getPackageInfoPN($postingNumber), $result->getStateOfPackage());
	}
	
	
	//formulář pro dotaz na balík
	protected function createComponentPackageQueryForm()
	{
		$form = new Form();
		$form->addText('podaciCislo', 'Podací číslo:', 45, 45)
		->setRequired('Je nutno zadat podací číslo zásilky');
		$form->addSubmit('odesli', 'Odeslat');
		$form->onSuccess[] = callback($this, 'packageQueryFormSubmitted');
		return $form;
	}
	
	
	//formulář pro vložení balíku
	protected function createComponentPackageNewForm(){
		$form = new Form();
		$form->addText('postingNumber', 'Podací číslo', 45,45)
		->setRequired();
		$role = array('SENDER' => 'Odesílatel', 'RECIPIENT' => 'Adresát');
		$form->addRadioList('role', 'Odesílatel/Příjemce:', $role);
		$form->addText('note', 'Poznámka', 100, 100);
		$form->addText('sender1', 'Odesílatel', 45, 45);
		$form->addText('sender2', 'Adresa', 45, 45);
		$form->addText('sender3', '', 45, 45);
		$form->addText('sender4', '', 45, 45);
		$form->addText('recipient1', 'Adresát', 45, 45);
		$form->addText('recipient2', 'Adresa', 45, 45);
		$form->addText('recipient3', '', 45, 45);
		$form->addText('recipient4', '', 45, 45);		
		$form->addText('cod', 'Dobírka')
		->setType('number');
		$form->addText('weight', 'Váha')
		->setType('number');
		$form->addSubmit('odesli', 'Odeslat');
		$form->onSuccess[] = callback($this, 'PackageNewFormSubmitted');
		return $form;
	}
	
	public function packageQueryFormSubmitted(Form $form)
	{
		$this->redirect('show', $form->values->podaciCislo);
	}
	
	public function packageNewFormSubmitted(Form $form)
	{
		
		//vytvoření balíku
		//příprava hodnot
		$package = (array (
			'postingNumber' => $form->values->postingNumber,
			'sender1' => $form->values->sender1,
			'sender2' => $form->values->sender2,
			'sender3' => $form->values->sender3,
			'sender4' => $form->values->sender4,
			'recipient1' => $form->values->recipient1,
			'recipient2' => $form->values->recipient2,
			'recipient3' => $form->values->recipient3,
			'recipient4' => $form->values->recipient4,
			'cod' => $form->values->cod,
			'weight' => $form->values->weight				
		));
		
		$serno = $this->packageModel->savePackage($package);
		
		//vytvoření role
		$serno = $this->roleModel->saveRole($serno, $form->values->role);
		$this->flashMessage('Zásilka vložena.', 'success');
		$this->redirect('packageList:default');
		
	}
}
